package cn.stronglink.asset.module.basicdata.assetTypeAttrGroup.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.stronglink.asset.model.AssetTypeAttr;
import cn.stronglink.asset.model.AssetTypeAttrGroup;
import cn.stronglink.asset.module.basicdata.assetTypeAttr.service.AssetTypeAttrService;
import cn.stronglink.asset.module.basicdata.assetTypeAttrGroup.service.AssetTypeAttrGroupService;
import cn.stronglink.suite.core.base.AbstractController;
import cn.stronglink.suite.core.exception.BusinessException;
import cn.stronglink.suite.core.log.annotation.OperateLog;
import cn.stronglink.suite.core.log.annotation.OperateLog.OpType;

@Controller
@RequestMapping(value = "basic/attrGroup")
public class AssetTypeAttrGroupController extends AbstractController {

	@Autowired
	private AssetTypeAttrGroupService assetTypeAttrGroupService;
	@Autowired
	private AssetTypeAttrService assetTypeAttrService;
	
	/**
	 * 查询组列表
	 * @param modelMap
	 * @param request
	 * @param response
	 * @param params
	 * @return
	 */
	@RequestMapping(value = "queryList")
	public ResponseEntity<ModelMap> queryList(ModelMap modelMap, HttpServletRequest request,
			HttpServletResponse response, @RequestBody Map<String, Object> params) {		
		List<AssetTypeAttrGroup> data = assetTypeAttrGroupService.queryAssetTypeAttrGroup(params);
		return setSuccessModelMap(modelMap, data);
	}
	/**
	 * 根据组查询组属性列表
	 * @param modelMap
	 * @param request
	 * @param response
	 * @param params
	 * @return
	 */
	@RequestMapping(value = "queryAttrByGroupId")
	public ResponseEntity<ModelMap> queryAttrByGroupId(ModelMap modelMap, HttpServletRequest request,
			HttpServletResponse response, @RequestBody Map<String, Object> params) {		
		List<AssetTypeAttr> data = assetTypeAttrService.queryAssetTypeAttr(params);
		return setSuccessModelMap(modelMap, data);
	}
	
	/**
	 * 查询组对象信息
	 * @param modelMap
	 * @param request
	 * @param response
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "qryById")
	public ResponseEntity<ModelMap> qryById(ModelMap modelMap, HttpServletRequest request,
			HttpServletResponse response, @RequestBody Long id) {		
	    AssetTypeAttrGroup data = assetTypeAttrGroupService.qryById(id);
		return setSuccessModelMap(modelMap, data);
	}
	
	/**
	 * 创建组
	 * @param modelMap
	 * @param request
	 * @param response
	 * @param assetTypeAttrGroup
	 * @return
	 */
	@OperateLog(module = "资产动态属性组管理",desc="添加资产动态属性组", type = OpType.ADD)
	@RequestMapping(value = "create")
	public ResponseEntity<ModelMap> create(ModelMap modelMap, HttpServletRequest request,
			HttpServletResponse response,@RequestBody AssetTypeAttrGroup assetTypeAttrGroup) {
		assetTypeAttrGroupService.insertAssetTypeAttrGroup(assetTypeAttrGroup);
		return setSuccessModelMap(modelMap, null);
	}
	
	/**
	 * 编辑组
	 * @param modelMap
	 * @param request
	 * @param response
	 * @param assetTypeAttrGroup
	 * @return
	 */
	@OperateLog(module = "资产动态属性组管理",desc="编辑资产动态属性组", type = OpType.UPDATE)
	@RequestMapping(value = "edit")
	public ResponseEntity<ModelMap> edit(ModelMap modelMap, HttpServletRequest request,
			HttpServletResponse response,@RequestBody AssetTypeAttrGroup assetTypeAttrGroup) {
		assetTypeAttrGroupService.insertAssetTypeAttrGroup(assetTypeAttrGroup);
		return setSuccessModelMap(modelMap, null);
	}
	
	/**
	 * 删除组
	 * @param modelMap
	 * @param request
	 * @param response
	 * @param id
	 * @return
	 */
	@OperateLog(module = "资产动态属性组管理",desc="删除资产动态属性组", type = OpType.DEL)
	@RequestMapping(value = "remove")
	public ResponseEntity<ModelMap> remove(ModelMap modelMap, HttpServletRequest request,
			HttpServletResponse response,@RequestBody Long id) {
		Integer count=assetTypeAttrGroupService.getGroupAttr(id);
		if(count > 0){		
			throw new BusinessException("组下包含属性，不能删除");
		}
		assetTypeAttrGroupService.removeAssetTypeAttrGroup(id);
		return setSuccessModelMap(modelMap, null);
	}
	
}
